package controller

import (
	"log"
	"testhttp/model"
	"testhttp/service"

	"github.com/kataras/iris/v12"
)

func ListTemp(ctx iris.Context) {
	var listparam model.HttpParamsOrderTemp
	page, err := ctx.URLParamInt("page")
	if err != nil {
		log.Print("Debug ListTemp:", page, err)
	}
	listparam.Page = page
	listparam.Pagesize, _ = ctx.URLParamInt("limit")

	ctx.JSON(service.ListTemp(listparam))

}
func TempListforSelect(ctx iris.Context) {
	ctx.JSON(service.TempListforSelect())
}

func Deltemp(ctx iris.Context) {
	var ordertemp []model.OrderTemp
	err := ctx.ReadJSON(&ordertemp)

	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
	} else {
		service.DelTemp(ordertemp)
	}
}

func Viewtemp(ctx iris.Context) {
	uid := ctx.URLParam("id")
	ctx.JSON(service.GetOrderTempByUid(uid))
}

func Addtemp(ctx iris.Context) {

	var ordertemp []model.OrderTemp
	err := ctx.ReadJSON(&ordertemp)

	if err != nil && !iris.IsErrPath(err) {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
		ctx.JSON(iris.Map{
			"code": 400,
			"msg":  "false",
		})
	} else {
		service.AddTemp(ordertemp)
	}
}
